package com.pinda.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinda.auth.entity.PdAuthRole;
import com.pinda.auth.entity.PdAuthUser;
import com.pinda.auth.entity.PdAuthUserRole;
import com.pinda.auth.search.PdAuthRoleSearch;
import com.pinda.auth.view.AuthorizationView;
import com.pinda.auth.view.ConfigView;
import com.pinda.commonutils.Page;
import com.pinda.mapper.*;
import com.pinda.service.IPdAuthRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author 黄杰
 * @since 2023-06-13
 */
@Service
public class PdAuthRoleServiceImpl extends ServiceImpl<PdAuthRoleMapper, PdAuthRole> implements IPdAuthRoleService {
    @Resource
    private PdAuthUserMapper pdAuthUserMapper;
    @Resource
    private PdAuthRoleMapper pdAuthRoleMapper;
    @Resource
    private PdAuthUserRoleMapper pdAuthUserRoleMapper;
    @Resource
    private PdAuthMenuMapper pdAuthMenuMapper;;
    @Resource
    private PdAuthResourceMapper pdAuthResourceMapper;
    @Resource
    private PdAuthRoleMenuMapper pdAuthRoleMenuMapper;
    @Resource
    private PdAuthRoleAuthorityMapper pdAuthRotaAuthorityMapper;

    @Override
    public PageInfo<PdAuthRole> findRoleService(Page<PdAuthRoleSearch> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        QueryWrapper<PdAuthRole> pdAuthRoleQueryWrapper = new QueryWrapper<PdAuthRole>();
        if(!StringUtils.isEmpty(page.getSearch().getCode())){
            pdAuthRoleQueryWrapper.eq("code",page.getSearch().getCode());
        }
        if(!ObjectUtils.isEmpty(page.getSearch().getCreateTime())){
            System.out.println(page.getSearch().getCreateTime());
            pdAuthRoleQueryWrapper.between( "create_time", page.getSearch().getCreateTime()[0], page.getSearch().getCreateTime()[1]);
        }
        if (!StringUtils.isEmpty(page.getSearch().getName())){
            pdAuthRoleQueryWrapper.like("name",page.getSearch().getName());
        }
        return new PageInfo<PdAuthRole>(this.pdAuthRoleMapper.selectList(pdAuthRoleQueryWrapper));
    }

    @Override
    public AuthorizationView findAuthorizationService(String roleId, String userId) {
        return AuthorizationView.builder()
                .users(this.pdAuthUserMapper.selectList(new QueryWrapper<PdAuthUser>().ne("id",userId)))
                .userId(this.pdAuthRoleMapper.findRoleByIdUser(roleId,userId)).build();
    }

    @Override
    public boolean batchAuthorizationService(AuthorizationView authorizationView) {
        boolean flag = false;
        //TODO 批量授予用户角色
        if(!authorizationView.getAdds().isEmpty()){
            flag=this.pdAuthUserRoleMapper.batchAddUserRole(authorizationView)>0;
        }
        //TODO 批量回收用户角色
        if(!authorizationView.getRemoves().isEmpty()){
            flag=this.pdAuthUserRoleMapper.batchRemoveUserRole(authorizationView)>0;
        }
        return flag;
    }

    @Override
    public ConfigView findConfigService(String roleId) {
        return ConfigView.builder()
                .menus(this.pdAuthMenuMapper.findRoleByIdMenu(roleId))
                .resources(this.pdAuthResourceMapper.findRoleByIdResource(roleId))
                .build();
    }

    @Override
    public boolean batchConfigService(ConfigView configView) {
        boolean flag = false;
        //TODO 批量添加角色菜单
        if(!configView.getAddMenu().isEmpty()){
            flag = this.pdAuthRoleMenuMapper.batchAddRoleMenu(configView)>0;
        }
        //TODO 批量删除角色菜单
        if(!configView.getRemoveMenu().isEmpty()){
            flag = this.pdAuthRoleMenuMapper.batchRemoveRoleMenu(configView)>0;
        }
        //TODO 批量添加角色资源
        if(!configView.getAddResources().isEmpty()){
            flag = this.pdAuthRotaAuthorityMapper.batchAddRoleAuthority(configView)>0;
        }
        //TODO 批量删除角色资源
        if(!configView.getRemoveResources().isEmpty()){
            flag = this.pdAuthRotaAuthorityMapper.batchRemoveRoleAuthority(configView)>0;
        }
        return flag;
    }

    @Override
    public boolean addRoleService(PdAuthRole pdAuthRole) {
        boolean flag = false;
        flag = this.pdAuthRoleMapper.addRole(pdAuthRole);
        flag = this.pdAuthUserRoleMapper.insert(PdAuthUserRole.
                builder()
                .roleId(pdAuthRole.getId())
                .userId(pdAuthRole.getCreateUser())
                .createUser(pdAuthRole.getCreateUser())
                .createTime(pdAuthRole.getCreateTime())
                .build())>0;
        return flag;
    }
}
